import pymysql

from .sql_def import ApiMysql

import datetime

class ApiMysqlclass(ApiMysql):

    def __init__(self):
        super().__init__()

    def current_openid(self, methods, openid):
        '''Implement functions with all the information in the openid table, parameter: user corresponds to openid'''

        if not methods == 'POST':
    
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }

        if not openid:

            return {
                'status': -1,
                'errorCode': 1,
                'errorMsg':f'请检查参数是否为空,openid:{openid},请重新输入',
                'resultBody':None
            }

        openid_table_info = self.open_id(openid)

        if not openid_table_info:

            return {
                'status': -1,
                'errorCode': 2,
                'errorMsg':f'请检查参数是否存在,openid:{openid},请重新输入',
                'resultBody':None
            }

        else:

            company_id = openid_table_info['company_id']

            pay = openid_table_info['pay']

            if company_id:

                company_update = self.company_info(company_id)['update_time']

                now = datetime.datetime.now()

                time_line = datetime.timedelta(seconds=3600*(24-8))

                company_state = 1

                if now-company_update > time_line:#可以更新

                    company_update = 1

                else:

                    company_update = 0
            
            else:
                company_state = 0

                company_update = 0


            return {
                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody':{'openid_state':
                    {'company_state':company_state, 
                    'pay_state':pay, 
                    'company_update_state':company_update}}
            }

            

        

    